001    /**
002     * Created by IntelliJ IDEA.
003     * User: Wei Wang
004     * Date: Nov 27, 2002
005     * Time: 10:19:18 PM
006     */
007    
008    package EVolve.util.painters;
009    
010    import EVolve.visualization.*;
011    import EVolve.Scene;
012    import EVolve.data.Entity;
013    
014    import java.awt.*;
015    import java.util.HashMap;
016    
017    public class PredictionPainter extends Painter{
018        protected Color colorRed = new Color(255, 0, 0);
019        protected Color colorBlue = new Color(120, 160, 255);
020        protected Predictor[] predictor; // predictors
021        protected int[] miss;
022        protected HashMap targetCheckMap;
023        protected int targetType;
024        private boolean furtherCheckNeeded = true;
025    
026        public PredictionPainter() {
027            targetCheckMap = new HashMap();
028        }
029    
030        public PredictionPainter(Predictor[] predictor, int targetType) {
031            this.predictor = predictor;
032            targetCheckMap = new HashMap();
033            this.targetType = targetType;
034            miss = new int[predictor.length];
035            for (int i = 0; i < predictor.length; i++) {
036                miss[i] = 0;
037            }
038        }
039    
040        public String getName() {
041            return "Prediction Painter";
042        }
043    
044        public void paint(AutoImage image, long x, long y, long z) {
045            predictor[(int)y].newTarget(z);
046    
047            if (validateTarget(y,z)) {
048                if (predictor[(int)y].isCorrect()) {
049                    if (image.getColor((int)x,(int)y) == null) {
050                        image.setColor((int)x,(int)y, colorBlue);
051                    }
052                } else {
053                    image.setColor((int)x, (int)y, colorRed);
054                    miss[(int)y]++;
055                }
056            } else {
057                if (image.getColor((int)x, (int)y) == null) {
058                    image.setColor((int)x, (int)y, colorBlue);
059                }
060            }
061        }
062    
063        public int[] getMiss() {
064            return miss;
065        }
066    
067        public void setPredictor(Predictor[] predictor, int targetType) {
068            this.predictor = predictor;
069            this.targetType = targetType;
070            miss = new int[predictor.length];
071            for (int i = 0; i < predictor.length; i++) {
072                miss[i] = 0;
073            }
074        }
075    
076        protected boolean validateTarget(long y, long z) {
077            String newTarget = null;
078            if (furtherCheckNeeded) {
079                newTarget = ((Entity)Scene.getDataManager().getEntity()[targetType].get(new Long(z))).getName();
080                if ((newTarget.indexOf('(') == -1) || // not a method
081                    (newTarget.indexOf('#') != -1) /*a location*/) {
082                    furtherCheckNeeded = false;
083                    return true;
084                }
085                String oldTarget = (String)targetCheckMap.get(new Long(y));
086                int index1 = newTarget.indexOf('(');
087                index1 = newTarget.substring(0,index1).lastIndexOf('.');
088                newTarget = newTarget.substring(index1+1);
089                targetCheckMap.put(new Long(y),newTarget);
090                if ((oldTarget!=null)&&(!oldTarget.equals(newTarget)))
091                    return false;
092            }
093            return true;
094        }
095    
096        public Object clone() {
097            PredictionPainter o = (PredictionPainter)super.clone();
098            o.colorRed = new Color(255,0,0);
099            o.colorBlue = new Color(120,160,255);
100            o.predictor = (predictor == null) ?  null : new Predictor[predictor.length];
101            o.miss = null;
102            if (miss != null) {
103                o.miss = new int[miss.length];
104                for (int i=0; i<miss.length; i++)
105                    o.miss[i] = miss[i];
106            }
107            return o;
108        }
109    }